<template>
  <div>
    <h3>商品添加</h3>

    <div class="productAdd">
      <p class="proinps">
        商品名称&emsp;<span class="productName"
          ><el-input placeholder="商品名称" v-model="value" clearable>
          </el-input
        ></span>
      </p>
      <p class="proinps">
        商品分类&emsp;<el-select v-model="value" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </p>
      <!-- 计数器 -->
      <p class="proinps">
        商品价格&emsp;<el-input-number
          size="medium"
          v-model="num"
        ></el-input-number>
      </p>

      <!-- 上传头像 -->
      <p class="proinps">
        商品头像&emsp;<el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </p>

      <!-- 商品描述 -->
      <p class="proinps">
        商品描述&emsp;
        <span class="textarea">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="textarea"
            maxlength="30"
            show-word-limit
          >
          </el-input>
        </span>
      </p>

      <!-- 添加商品 -->
      <el-button type="primary" class="addPro">添加商品</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "选项1",
          label: "黄金糕",
        },
        {
          value: "选项2",
          label: "双皮奶",
        },
        {
          value: "选项3",
          label: "蚵仔煎",
        },
        {
          value: "选项4",
          label: "龙须面",
        },
        {
          value: "选项5",
          label: "北京烤鸭",
        },
      ],
      value: "",
      num: "1",
      textarea: "",
      imageUrl: '',
    };
  },
  methods: {
    // 上传头像-----------
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    // -------------上传头像

  },
};
</script>

<style lang="less" scoped>
h3 {
  height: 50px;
  display: flex;
  align-items: center;
}
.productAdd {
  padding: 20px;
  width: 100%;
  background-color: white;
  display: flex;
  flex-direction: column;
  .proinps {
    display: flex;
    align-items: center;
    margin-top: 20px;
    .productName {
      display: flex;
      width: 300px;
    }
    .textarea {
      display: block;
      width: 300px;
    }
  }
  .addPro {
    margin-top: 20px;
    margin-left: 80px;
    width: 100px;
  }
}
// 头像样式-----
.avatar-uploader {
  border: 1px dashed #d9d9d9;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
// -----头像样式
</style>